package dailyPic;

/*
 * 做成swing界面的，在swing界面中打开图片
 * 在一个textfield中输入要重新命的名字
 * 提供一个取消按钮
 */
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import javax.swing.JOptionPane;

public class DailyPic {
	File renamedPicFile = new File("/home/tarena/Pictures/picList");//己经重命名的图片保存在磁盘上的记录
	List<File> renamed;//从picList得到的己经重命名的图片
	List<File> picsList = new ArrayList<File>();//未重命名的图片集合
  File picture;//是否要重命名的文件
	
	public String getPic() throws Exception {
		//随机获取一张图片
		int size = picsList.size();
		Random r = new Random();
		int pic = r.nextInt(size);
		picture = picsList.get(pic);
		//在firefox中打开此图片
		//Runtime.getRuntime().exec("/usr/bin/firefox" + " -url " + picture);
		return picture.getAbsolutePath();
	}
  public void rename(String name){
		File newFile = new File(picture.getParentFile() + "/" + name);
    
    if (renamed.contains(newFile)) {
      JOptionPane.showMessageDialog(null, "文件己存在");
      return;
    }
    picsList.remove(picture);
    picture.renameTo(newFile);
		renamed.add(newFile);

		//String confirm = null;
		//if (renamed.contains(newFile)) {
			//System.out.println("文件已存在，是否覆盖？");
		//}
		//if ("yes".equals(confirm) || !"no".equals(name)) {
			//picture.renameTo(newFile);
			//renamed.add(newFile);
		//}
  }
  public void saveLog() throws IOException{
    ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(
				renamedPicFile));
		out.writeObject(renamed);
  }

	public void getUnrenamedImgList() throws Exception {
		//获得己命名图片集合
		if (renamedPicFile.exists()) {
			ObjectInputStream in = new ObjectInputStream(new FileInputStream(renamedPicFile));
			renamed = (List<File>) in.readObject();
		} else {
			renamed = new ArrayList<File>();
		}
		//获得图片文件夹中的全部文件
		String path = "/home/tarena/Pictures";
		File picDir = new File(path);
		File[] pics = picDir.listFiles();
		for (int i = 0; i < pics.length; i++) {
			picsList.add(pics[i]);
		}
		//图片集合中不包括己重命名的图片保存记录文件
		picsList.remove(renamedPicFile);
		//获得未重命名的图片集合
		for (Iterator iter = picsList.iterator(); iter.hasNext();) {
			File f = (File) iter.next();
			if (renamed.contains(f))
				iter.remove();
		}
	}
}
